package thread;

/**
 * 互斥锁
 */
public class SyncDemo4 {
    public static void main(String[] args) {
        Boo boo = new Boo();
        Boo b1 = new Boo();
        Thread t1 = new Thread() {
            public void run() {
                boo.methodA();
            }
        };

        Thread t2 = new Thread() {
            public void run() {
                boo.methodB();
            }
        };
        t1.start();
        t2.start();
    }
}

class Boo {
    public void methodA() {
        synchronized (this) {
            try {
                Thread t = Thread.currentThread();
                System.out.println(t.getName() + ":调用了A方法");
                Thread.sleep(5000);
                System.out.println(t.getName() + ":调用A方法结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public synchronized void methodB() {
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName() + ":调用了B方法");
            Thread.sleep(5000);
            System.out.println(t.getName() + ":调用B方法结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}
